#!/usr/bin/env python
# coding=utf-8
from flask import Flask, request
from adapter import MarkDownAdapter
from vuepress import VuePress
app = Flask(__name__)

reporters:dict[str,MarkDownAdapter] = {}

@app.route("/report/<time>",methods=["POST"])
def data_post(time:str):
    try:
        data = request.get_json(force=True)
        seq = data["seq"]
        print(seq)
        ret = f"receive counter:{seq}"
        code = 200
        print(ret)
        if seq == 0:
            reporters[time]=VuePress(time,data)
        elif seq == -1:
            if time in reporters.keys():
                reporters[time].submit()
                print(f"{time}.md is finished")
                ret = "提交报告完成"
                reporters.pop(time)
            else:
                ret+=" not start this report!\nsubmit denied!"
                code = 400
        else:
            if time in reporters.keys():
                reporters[time].push(seq,data["src"],data["detect"],data["defects"])
            else:
                ret+=" not start this report!\npush denied!"
                code = 400
        return ret,code
    except Exception as e:
        print(e)
        return "incorrect format! need json only",400
if __name__=="__main__":
    app.run(host="127.0.0.1",port=5000)